import json
from pathlib import Path
from typing import Dict, Set, List


class ConfigManager:
    def __init__(self):
        self.config_file = Path('configs.json')
        self.active_keys: Set[int] = set()
        self.data: Dict[str: List[int]] = dict()

        if self.config_file.exists():
            self.__load_config()
        else:
            self.save()

    def save(self):
        data = {
            'active_keys': list(self.active_keys),
            'config': self.data
        }
        try:
            with open(self.config_file, 'w', encoding='utf-8') as f:
                json.dump(data, f, ensure_ascii=False, indent=4)
        except Exception as e:
            print(e)

    def __load_config(self):
        try:
            with open(self.config_file, 'r', encoding='utf-8') as f:
                data = json.load(f)
                self.active_keys = set(data['active_keys'])
                self.data = data['config']
        except Exception as e:
            print(e)

    def change_active_key(self, name: str):
        if keys := self.data.get(name):
            self.active_keys = set(keys)
